namespace ApplicationControllerExample.Infrastructure
{
    using System;
    using EventAggregator;
    using StructureMap;
    using StructureMap.Interceptors;

    public class EventAggregatorInterceptor : TypeInterceptor
    {
        #region TypeInterceptor Members

        public object Process(object target, IContext context)
        {
            var eventPublisher = context.GetInstance<IEventPublisher>();
            eventPublisher.RegisterHandlers(target);
            return target;
        }

        public bool MatchesType(Type type)
        {
            bool matchesType = type.ImplementsInterfaceTemplate(typeof (IEventHandler<>));
            return matchesType;
        }

        #endregion
    }
}